<template>
  <!-- 获取不到总页数 -->
  <div class="page-div" v-if="true">
    <div class="total">
      当前页有<i>{{paging.total}}</i>条记录
    </div>
    <div class="btn">
      <el-button type="primary" plain :disabled="paging.prevDisabled" @click="pageChange(paging.index-1)">上一页</el-button>
      <el-text class="text" size="large" type="primary" tag="b">{{paging.index}}</el-text>
      <el-button type="primary" plain :disabled="paging.nextDisabled" @click="pageChange(paging.index+1)">下一页</el-button>
    </div>
  </div>
  <!-- 能获取到total 总页数 -->
  <div class="page-div" v-if="false">
    <div class="total">
      共<i>{{paging.total}}</i>条记录
    </div>
    <!-- <el-select
      v-model="paging.size"
      placeholder="请选择"
      popper-class="page-option"
    >
      <el-option
        v-for="item in paging.sizeOptions"
        :key="item.value"
        :value="item.value"
        :label="item.label"
        :disabled="item.disabled"
      >
        <span style="display: inline-block; width: 100%; text-align: center">{{
          item.label
        }}</span>
      </el-option>
    </el-select> -->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="pageChange"
      :pager-count="9"
      :page-size="paging.size"
      :total="paging.total"
      prev-text="上一页"
      next-text="下一页"
    >
    </el-pagination>
  </div>
</template>

<script>
import { reactive } from 'vue';
export default {
  setup(props, ctx) {
    let paging = reactive({
      total: 0,
      index: 1,
      size: 50,
      sizeOptions: [
        { value: 10, label: "10条/页", disabled: false },
        { value: 50, label: "50条/页", disabled: false },
        { value: 100, label: "100条/页", disabled: false },
        { value: 500, label: "500条/页", disabled: true },
      ],
      prevDisabled: true,
      nextDisabled: false,
    });
    
    function setData(data) {
    //console.log(data)
      let s = data.size
      paging.total = data.total
      paging.size = s 
      if (('10,50,100,500').indexOf(s)<0) paging.sizeOptions.unshift({ value: s, label: `${s}条/页`, disabled: false })
      // 获取不到总页数
      paging.prevDisabled = paging.index <=1
      paging.nextDisabled = paging.total < paging.size
    }

    function pageChange(val) {
      // console.log(val)
      paging.index = val;
      ctx.emit('change',val)
    }

    return {
      paging,
      setData,
      pageChange,
    };
  },
};
</script>

<style lang="scss" scoped>
// 分页
.page-div {
  @extend %view-flex;
  flex-direction: row;
  height: 60px;
  justify-content: space-between;
  .total{
    font-size: 16px;
    color: #999;
    padding: 0 3px;
    i{
      font-style: normal;
      font-weight: 900;
      background-color: $color-primary;
      color: #fff;
      padding: 3px 8px;
      margin: 0 3px;
      border-radius: 3px;
    }
  }
  .btn{
    .text{
      padding: 0 15px;
      font-size: 16px;
    }
  }
}
</style>